﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using _InformaticaIndustrial_DerivdosVinicos.DTO;
using Dao.Intermediarios;
using Dao;


namespace _InformaticaIndustrial_DerivdosVinicos.Interfaz
{
    public partial class CGI : Form
    {
        double cgi = 0;
        private MateriaPrima mp;
        private Componente comp;
        private Demanda dem;
        private List<Demanda> demandalist;
        private ParametrosDemanda pd;
        
        public CGI()
        {
            InitializeComponent();
            LoadCombo();
        }

        private void LoadCombo()
        {
            cbTipoArt.DataSource = new IPTipoMaestroArticulo().getAll<TipoMaestroArticulo>().Where(ta => ta.codTipo != "03").ToList();
            cbTipoArt.DisplayMember = "nombreTipo";
            cbTipoArt.ValueMember = "IdTP";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                demandalist = new IPDemandaCGI().getByCriterio<Demanda>("", "", cbArt.SelectedValue.ToString());

                if (demandalist.Count > 0)
                {
                    dem = demandalist.Last();

                    if (Convert.ToInt16(1) == Convert.ToInt16(cbTipoArt.SelectedValue)) //Es MP
                    {
                       mp = (MateriaPrima)cbArt.SelectedItem;
                       cgi = (double)((mp.costo * dem.demandaEstimada) + mp.CostoAlmacenamiento + mp.CostoPedido);
                        
                    }
                    else if (Convert.ToInt16(2) == Convert.ToInt16(cbTipoArt.SelectedValue))
                    {
                        comp = (Componente)cbArt.SelectedItem;
                        cgi = (double)((comp.costo * dem.demandaEstimada) + comp.CostoAlmacenamiento + comp.CostoPedido);
                    }

                    txtCGI.Text = cgi.ToString();
                }
                else
                {
                    MessageBox.Show("No existe demanda para el elemento seleccionado");
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        private void btCancelar_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btGuardar_Click(object sender, EventArgs e)
        {
            try
            {
                if (Convert.ToInt16(1) == Convert.ToInt16(cbTipoArt.SelectedValue)) //Es MP
                {
                    mp.CGI = txtCGI.Text;
                }
                else if (Convert.ToInt16(2) == Convert.ToInt16(cbTipoArt.SelectedValue))
                {
                    comp.CGI = txtCGI.Text;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        private void cbArt_SelectedIndexChanged(object sender, EventArgs e)
        {

            //btGuardar.Enabled = true;
            txtCGI.Text = "";
            txtD.Text = "";
            txtCtoCpra.Text = "";
            txtCP.Text = "";
            txtCA.Text = "";

        }

        private void cbTipoArt_SelectedValueChanged(object sender, EventArgs e)
        {
            txtCGI.Text = "";
            txtD.Text = "";
            txtCtoCpra.Text = "";
            txtCP.Text = "";
            txtCA.Text = "";

            try
            {
                if (Convert.ToInt16(1) == Convert.ToInt16(cbTipoArt.SelectedValue)) //Es MP
                {
                    cbArt.DataSource = new IPMateriaPrima().getAll<MateriaPrima>();
                    cbArt.DisplayMember = "nombre";
                    cbArt.ValueMember = "IdMP";
                }
                else if (Convert.ToInt16(2) == Convert.ToInt16(cbTipoArt.SelectedValue))
                {
                    cbArt.DataSource = new IPComponente().getAll<Componente>();
                    cbArt.DisplayMember = "nombre";
                    cbArt.ValueMember = "IdComponente";
                }
                else
                {
                    cbArt.DataSource = new IPProductoProcesoElaboracion().getAll<ProductoProcesoElaboracion>();
                    cbArt.DisplayMember = "nombre";
                    cbArt.ValueMember = "IdPPE";
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
